//
//  SuperView.m
//  location
//
//  Created by Dongxiang Xu on 2025/9/4.
//

#import "MapSuperView.h"
#import "XDXMapView.h"
#import "HitTouchView.h"
#import "NotifyView.h"
#import "XDXLocationManager.h"

// 创建这个view是为将地图和轨迹都放在这个view上，来转移手势的事件
@implementation MapSuperView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([[XDXLocationManager shareInstance].locationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways || [[XDXLocationManager shareInstance].locationManager accuracyAuthorization]==CLAccuracyAuthorizationReducedAccuracy) {
            NotifyView *view = [[NotifyView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            [WINDOW addSubview:view];
        }        
    }
    return self;
}

#pragma mark - private method
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{   
    // 如果事件响应的是轨迹页面的空白部分，将事件传递给XDXMap的MKAnnotationContainerView，这个效果给我整麻了
    UIView *view = [super hitTest:point withEvent:event];
    if ([view isKindOfClass:[HitTouchView class]]) {
        for (NSDictionary *dic in self.unVisibleLocations.allValues) {
            if (CGRectContainsPoint(((UIView *)dic[@"view"]).frame, point)) {
                return ((UIView *)dic[@"view"]);
            }
        }
                
        for (UIView *view in self.subviews) {
            if ([view isKindOfClass:[XDXMapView class]]) {
                for (UIView *aView in view.subviews) {
                    NSString *className1 = @"_MKMapContentView";
                    NSString *className = [NSString stringWithCString:object_getClassName(aView) encoding:NSUTF8StringEncoding];
                    if ([className1 isEqualToString:className]) {
                        for (UIView *aaView in aView.subviews) {
                            NSString *className1 = @"MKAnnotationContainerView";
                            NSString *className = [NSString stringWithCString:object_getClassName(aaView) encoding:NSUTF8StringEncoding];
                            if ([className1 isEqualToString:className]) {
                                return aaView;
                            }
                        }
                    }
                }
            }
        }
    }
    return view;
}

@end
